package com.demo.webmagic.pipeline;

import java.util.List;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Flightinfo;
import com.demo.common.model.Flightno;
import com.demo.common.model.Flighttrends;
import com.demo.webmagic.dto.DtoFlightInfo;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class PipelineVeryZhunFlightInfo implements Pipeline {

	@Override
	public void process(ResultItems resultItems, Task task) {
		DtoFlightInfo dtoFlightInfo = resultItems.get("result");
		if(dtoFlightInfo != null) {
			if(dtoFlightInfo.getCode() == ConstantDtoFlight.code_success) {
				Flightinfo flightInfo = dtoFlightInfo.getFlightInfo();
				List<Flighttrends> trendsList = dtoFlightInfo.getTrendsList();
				if(flightInfo != null && trendsList!=null && trendsList.size() > 0) {
					if(flightInfo.save()) {
						for (Flighttrends flighttrends : trendsList) {
							flighttrends.setInfoId(flightInfo.getId());
							flighttrends.save();
						}
					}
				}
			} else if (dtoFlightInfo.getCode() == ConstantDtoFlight.code_fno_invalid) {
				Flightno flightno = Flightno.dao.findFirst("SELECT * FROM flightno WHERE fno = ?", dtoFlightInfo.getFno());
				if(flightno != null) {
					flightno.delete();
				}
			}
		}
	}
	
}
